﻿using System.Net;

namespace ThingsGateway.Application.Core;
public static class ReadWriteHelpers
{


    public static object ObjToTypeValue(PropertyInfo p, DeviceProperty setting)
    {
        object value = null;
        if (p.PropertyType == typeof(bool))
            value = setting.Value.GetBoolValue();
        else if (p.PropertyType == typeof(byte))
            value = byte.Parse(setting.Value);
        else if (p.PropertyType == typeof(sbyte))
            value = sbyte.Parse(setting.Value);
        else if (p.PropertyType == typeof(short))
            value = short.Parse(setting.Value);
        else if (p.PropertyType == typeof(ushort))
            value = ushort.Parse(setting.Value);
        else if (p.PropertyType == typeof(int))
            value = int.Parse(setting.Value);
        else if (p.PropertyType == typeof(uint))
            value = uint.Parse(setting.Value);
        else if (p.PropertyType == typeof(long))
            value = long.Parse(setting.Value);
        else if (p.PropertyType == typeof(ulong))
            value = ulong.Parse(setting.Value);
        else if (p.PropertyType == typeof(float))
            value = float.Parse(setting.Value);
        else if (p.PropertyType == typeof(double))
            value = double.Parse(setting.Value);
        else if (p.PropertyType == typeof(decimal))
            value = decimal.Parse(setting.Value);
        else if (p.PropertyType == typeof(DateTime))
            value = DateTime.Parse(setting.Value);
        else if (p.PropertyType == typeof(string))
            value = setting.Value;
        else if (p.PropertyType == typeof(IPAddress))
            value = IPAddress.Parse(setting.Value);
        else if (p.PropertyType.BaseType == typeof(Enum))
            value = Enum.Parse(p.PropertyType, setting.Value);
        return value;
    }


    /// <summary>
    /// 根据<see cref="ThingsGateway.Foundation.Result.IsSuccess"/>执行action
    /// </summary>
    public static Result<T> DealWithReadResult<T>(Result<T> read, Action<T> action)
    {

        if (!read.IsSuccess || action == null)
            return read;
        action(read.Content);
        return read;
    }

    /// <summary>
    /// 在返回的字节数组中解析每个变量的值
    /// 根据每个变量的<see cref="DeviceVariable.Index"/>
    /// 不支持变长字符串类型变量，一定不能存在于变量List中
    /// </summary>
    /// <param name="buffer">返回的字节数组</param>
    /// <param name="values">设备变量List</param>
    /// <param name="deviceByteConverter">设备默认转换</param>
    /// <param name="startIndex">开始序号</param>
    public static void PraseStructContent(
              byte[] buffer,
              IList<DeviceVariable> values,
              IByteConverter deviceByteConverter, int startIndex = 0)
    {
        foreach (DeviceVariable organizedVariable in values)
        {
            var deviceValue = organizedVariable;
            string address = deviceValue.VariableAddress;
            //从变量地址中获取DataFormat，得到新的ByteConverter
            //IByteConverter byteConverter = ByteConverterHelper.ExtractTransformParameter(ref address, deviceByteConverter);
            IByteConverter byteConverter = ByteConverterHelper.ExtractTransformParameter(ref address, deviceValue.ByteConverter);
            Type propertyType = organizedVariable.DataType;
            int index = organizedVariable.Index;
            if (propertyType == typeof(byte))
            {
                byte num = byteConverter.ToByte(buffer, index + startIndex);
                organizedVariable.Value = num;
            }
            else if (propertyType == typeof(short))
            {
                short num = byteConverter.ToInt16(buffer, index + startIndex);
                organizedVariable.Value = num;
            }
            else if (propertyType == typeof(ushort))
            {
                ushort num = byteConverter.ToUInt16(buffer, index + startIndex);
                organizedVariable.Value = num;
            }
            else if (propertyType == typeof(int))
            {
                int num = byteConverter.ToInt32(buffer, index + startIndex);
                organizedVariable.Value = num;
            }
            else if (propertyType == typeof(uint))
            {
                uint num = byteConverter.ToUInt32(buffer, index + startIndex);
                organizedVariable.Value = num;
            }
            else if (propertyType == typeof(long))
            {
                long num = byteConverter.ToInt64(buffer, index + startIndex);
                organizedVariable.Value = num;
            }
            else if (propertyType == typeof(ulong))
            {
                ulong num = byteConverter.ToUInt64(buffer, index + startIndex);
                organizedVariable.Value = num;
            }
            else if (propertyType == typeof(float))
            {
                float num = byteConverter.ToSingle(buffer, index + startIndex);
                organizedVariable.Value = num;
            }
            else if (propertyType == typeof(double))
            {
                double num = byteConverter.ToDouble(buffer, index + startIndex);
                organizedVariable.Value = num;
            }
            else if (propertyType == typeof(bool))
            {
                bool num = byteConverter.ToBoolean(buffer, index + (startIndex * 8));
                organizedVariable.Value = num;
            }
            else if (propertyType == typeof(string))
            {
                //从变量地址中获取DataFormat，得到新的ByteConverter和字符串信息
                if (deviceValue.CoreDataType == CoreDataType.String)
                {
                    //                    string stringValueaddress = deviceValue.VariableAddress;
                    //                    IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(
                    //ref stringValueaddress, deviceByteConverter, out Encoding textEncoding, out ushort length, out BCDFormat bCDFormat
                    //);

                    string num = byteConverter.ToString(buffer, index + startIndex, deviceValue.StringLength, deviceValue.StringEncoding);
                    organizedVariable.Value = num;
                }
                else if (deviceValue.CoreDataType == CoreDataType.Bcd)
                {
                    //                        string stringValueaddress = deviceValue.VariableAddress;
                    //                        IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(
                    //ref stringValueaddress, deviceByteConverter, out Encoding textEncoding, out ushort length, out BCDFormat bCDFormat
                    //);
                    string num = byteConverter.ToBcdString(buffer, index + startIndex, deviceValue.StringLength, deviceValue.StringBCDFormat);
                    organizedVariable.Value = num;
                }
                else if (deviceValue.CoreDataType == CoreDataType.DateTime)
                {
                    //                        string stringValueaddress = deviceValue.VariableAddress;
                    //                        IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(
                    //ref stringValueaddress, deviceByteConverter, out Encoding textEncoding, out ushort length, out BCDFormat bCDFormat
                    //);
                    DateTime num = DateTime.Parse(byteConverter.ToString(buffer, index + startIndex, deviceValue.StringLength, deviceValue.StringEncoding));
                    organizedVariable.Value = num;
                }

            }

        }
    }
}
